package com.ruoyi.develop.goodsSaleMaterialMain.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSaleMaterialChild.domain.GoodsSaleMaterialChild;
import com.ruoyi.develop.goodsSaleMaterialChild.service.IGoodsSaleMaterialChildService;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSaleMaterialMain.mapper.GoodsSaleMaterialMainMapper;
import com.ruoyi.develop.goodsSaleMaterialMain.domain.GoodsSaleMaterialMain;
import com.ruoyi.develop.goodsSaleMaterialMain.service.IGoodsSaleMaterialMainService;

/**
 * 领料单Service业务层处理
 * @author lmm
 * @date 2025-08-18
 */
@Service
public class GoodsSaleMaterialMainServiceImpl implements IGoodsSaleMaterialMainService {
    @Resource
    private GoodsSaleMaterialMainMapper goodsSaleMaterialMainMapper;
    @Resource
    private IGoodsSaleMaterialChildService goodsSaleMaterialChildService;

    /**
     * 查询领料单
     * @param id 领料单主键
     * @return 领料单
     */
    @Override
    public GoodsSaleMaterialMain selectById(String id) {
        return goodsSaleMaterialMainMapper.selectById(id);
    }

    /**
     * 查询领料单列表
     * @param entity 领料单
     * @return 领料单
     */
    @Override
    public List<GoodsSaleMaterialMain> selectList(GoodsSaleMaterialMain entity) {
        return goodsSaleMaterialMainMapper.selectList(entity);
    }

    /**
     * 新增领料单
     * @param entity 领料单
     * @return 结果
     */
    @Override
    public int insert(GoodsSaleMaterialMain entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }

        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());

        return goodsSaleMaterialMainMapper.insert(entity);
    }

    @Override
    public int addMain(GoodsSaleMaterialMain entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = goodsSaleMaterialMainMapper.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            String code=codeMaxString.replace("LL","");
            codeMaxString = "LL"+(Long.parseLong(code)+1)+"";
        }else{
            codeMaxString = "LL"+today+"0001";
        }
        entity.setCode(codeMaxString);
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        goodsSaleMaterialMainMapper.insert(entity);
        List<GoodsSaleMaterialChild> goodsSaleMaterialChildList=entity.getGoodsSaleMaterialChild();
        for(GoodsSaleMaterialChild goodsSaleMaterialChild:goodsSaleMaterialChildList){
            goodsSaleMaterialChild.setParentId(entity.getId());
            goodsSaleMaterialChild.setLingliaoNo(codeMaxString);
            goodsSaleMaterialChild.setStatus(BusinessConstants.LINGLIAO_1);
            goodsSaleMaterialChildService.update(goodsSaleMaterialChild);
        }
        return 1 ;
    }

    /**
     * 修改领料单
     * @param entity 领料单
     * @return 结果
     */
    @Override
    public int update(GoodsSaleMaterialMain entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSaleMaterialMainMapper.update(entity);
    }

    /**
     * 批量删除领料单
     * @param ids 需要删除的领料单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSaleMaterialMainMapper.deleteByIds(ids);
    }

    /**
     * 删除领料单信息
     * @param id 领料单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSaleMaterialMainMapper.deleteById(id);
    }
}
